﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Lm.Common.DAL.ModelCodeGenerator
{
    class ModelParser : IModelParser
    {
        public List<Model> Parse(System.Data.DataTable dt)
        {
            var data = from DataRow r in dt.Rows
                       group r by r["表名"].ToString() into g
                       select g;

            var result = new List<Model>();
            foreach (var d in data)
            {
                var model = new Model()
                {
                    ModelPropertyList = new List<ModelProperty>()
                };
                result.Add(model);
                model.Name = d.Key;
                foreach (var c in d)
                {
                    model.ModelPropertyList.Add(new ModelProperty()
                    {
                        CanNull = c["是否为空"].ToString() == "√",
                        Name = c["列名"].ToString(),
                        Type = c["类型"].ToString()
                    });
                }
            }
            return result;
        }
    }
}
